$(function(){
	
	var ajaxOP = new AjaxOperator();
	
	$("#searchDish").on("click",function(){
		var type = $("#dtype").val();
		var dishName = $("#dname").val();
		var dish={"dtype":type,"dname":dishName,"flag":"second"};
		ajaxOP.sendPost("search", dish,pareState, error);
	});
	
	$("button[dish_id]").on("click",function(){
		var did=$(this).attr("dish_id");
		var url="../dishes/delete.action?dId="+did;
		$.ajax({
			type:"GET",
			url:url
		}).done(function(msg){
			if(msg.flag){
				var did=msg.data;
				var selector="button[dish_id="+did+"]";
				$(selector).parent().parent().remove();
			}else{
				alert("Delete fail!");
			}
		}).fail(function(msg){
			alert("request fail!");
		});
		
	});
	
	
	function pareState(data){
		var dishBody = $("#dishbody");
		dishBody.empty();
		var types=["全部","酒水","主食","小吃","套餐"];
		data.forEach((item)=>{
			var tr = $("<tr>");
			var td1 = $("<td>");
			var img = $("<img>");
			img.attr("src","../../local/"+item.dImage);
			td1.append(img);
			var td2 = $("<td>").text(item.dName);
			var type = item.dType;
			var td3 = $("<td>").text(types[type]);
			var td4 = $("<td>").text(item.dPrice);
			var td5 = $("<td>");
			var a5 = $("<a>").text("更新");
			a5.attr("href","update?dId="+item.dId);
			td5.append(a5);
			var td6 = $("<td>");
			var button6 = $("<button>").text("删除");
			button6.attr("dish-id",item.dId);
			td6.append(button6);
			tr.append(td1);
			tr.append(td2);
			tr.append(td3);
			tr.append(td4);
			tr.append(td5);
			tr.append(td6);
			dishBody.append(tr);
		});
		
		
	}
	
	function error(data){
		
	}
	
});